<template>
  <view class="warp_box">
    <view class="yihang">
      <view class="x1">
        <img src="../../static/chuku/title_border.png" alt="" />
        <text>客户</text></view
      >
      <view class="x2">
        <text>张三</text> <img src="../../static/chuku/black_arrow.png" alt=""
      /></view>
    </view>
    <view class="erhang">
      <view class="left" @click="tohuopin_fn">
        <img src="../../static/chuku/add_icon_yellow1.png" alt="" />
        <text>添加货品</text>
      </view>
      <view class="right">
        <img src="../../static/chuku/scan_icon.png" alt="" />
        <text @click="sao_ma_fn()">扫条码</text>
      </view>
    </view>
    <view class="content">
      <view
        v-for="(item, index) in canshu"
        :key="item.id"
        class="neironghang"
        v-show="info"
      >
        <view class="neironghangyihang">
          <view class="left">
            <img :src="`http://localhost:3006/${item.good.img}`" alt="" />
          </view>
          <view class="right">
            <view class=""> 货品名称：{{ item.good.name }} </view>
            <view class=""> 货品编号：{{ item.id }} </view>
          </view>
        </view>
        <view class="neironghangerhang">
          <view class="x1"> M码 </view>
          <view class="x2"> 价格：{{ item.good.price }} </view>
          <view class="x3">
            <img
              @click="jian_fn(index)"
              src="../../static/chuku/minus_icon.png"
              alt=""
            />
            <text> {{ item.num }} </text>
            <img
              @click="jia_fn(index)"
              src="../../static/chuku/plus_icon.png"
              alt=""
            />
          </view>
        </view>
        <view @click="shanchuhuopin_fn(index)" class="neironghangsanhang">
          <img src="../../static/chuku/close_red.png" alt="" />
        </view>
      </view>
    </view>
    <view class="sanhang">
      <view class="x1">
        <img src="../../static/chuku/title_border.png" alt="" />
        <text class="xx1"> 货品合计 </text>
      </view>
      <view class="x2">
        <text class="xx2"> {{ chuhuojiage }} </text>
      </view>
    </view>
    <view class="sanhang1">
      <view class="x1">
        <text> 开单人 </text>
      </view>
      <view class="x2">
        <text> 李思 </text>
      </view>
    </view>
    <view class="sanhang1">
      <view class="x1">
        <text> 开单日期 </text>
      </view>
      <view class="x2">
        <text> 2020-10-20 </text>
      </view>
    </view>
    <view class="sihang">
      <view class="x1">
        <img src="../../static/chuku/title_border.png" alt="" />
        <text> 备注 </text>
      </view>
      <textarea
        placeholder="请输入"
        name=""
        id=""
        cols="30"
        rows="10"
      ></textarea>
    </view>
    <view class="bottom">
      <button @click="tochukudan_fn">保存出库单</button>
    </view>
  </view>
</template>
<script>
export default {
  components: {},
  data() {
    return {
      info: false,
      goods: "",
      canshu: "",
      chuhuoshulaing: "",
      chuhuojiage: "",
    };
  },
  methods: {
    jian_fn(index) {
      console.log(index);
      if (this.canshu[index].num > 0) {
        console.log(this.canshu[index]);
        this.canshu[index].num--;
        this.canshu[index].good.num++;
        this.canshu[index].zongjia =
          this.canshu[index].num * this.canshu[index].good.price;
        uni.request({
          url: "http://localhost:3006/xiugaikucun",
          data: {
            num: this.canshu[index].good.num,
            id: this.canshu[index].good.id,
          },
          header: {
            "custom-header": "hello",
          },
          success: (res) => {
            console.log("数据请求成功。。。。。。。");
            this.canshu[index].good.num = res.data.goods_info.num;
            uni.setStorage({
              key: "storage_key",
              data: this.canshu,
              success: function () {
                console.log("相同货品存储成功");
              },
            });
          },
        });
        var zongjia11 = 0;
        for (let i = 0; i < this.canshu.length; i++) {
          zongjia11 = zongjia11 + this.canshu[i].zongjia;
        }
        this.chuhuojiage = zongjia11;
      }
    },
    jia_fn(index) {
      if (this.canshu[index].num < this.canshu[index].goodnum) {
        console.log(this.canshu[index]);
        this.canshu[index].num++;
        this.canshu[index].good.num--;
        this.canshu[index].zongjia =
          this.canshu[index].num * this.canshu[index].good.price;
        uni.request({
          url: "http://localhost:3006/xiugaikucun",
          data: {
            num: this.canshu[index].good.num,
            id: this.canshu[index].good.id,
          },
          header: {
            "custom-header": "hello",
          },
          success: (res) => {
            console.log("数据请求成功。。。。。。。");
            this.canshu[index].good.num = res.data.goods_info.num;
            uni.setStorage({
              key: "storage_key",
              data: this.canshu,
              success: function () {
                console.log("相同货品存储成功");
              },
            });
          },
        });
        var zongjia11 = 0;
        for (let i = 0; i < this.canshu.length; i++) {
          zongjia11 = zongjia11 + this.canshu[i].zongjia;
        }
        this.chuhuojiage = zongjia11;
      }
    },
    shanchuhuopin_fn(index) {
      console.log(index);
      var value = uni.getStorageSync("storage_key");
      value.splice(index, 1);
      uni.setStorage({
        key: "storage_key",
        data: value,
        success: function () {
          console.log("删除单项货品成功");
        },
      });
      var zongjia11 = 0;
      for (let i = 0; i < value.length; i++) {
        zongjia11 = zongjia11 + value[i].zongjia;
      }
      this.chuhuojiage = zongjia11;
      this.canshu = value;
    },

    sao_ma_fn() {
      console.log("开始扫码.....");
      uni.scanCode({
        success: function (res) {
          console.log("条码类型：" + res.scanType);
          console.log("条码内容：" + res.result);
        },
      });
    },
    tochukudan_fn() {
      uni.navigateTo({
        url: "./chukudan",
      });
    },
    tohuopin_fn() {
      uni.navigateTo({
        url: "./xuanze",
      });
    },
  },
  created() {
    try {
      const value = uni.getStorageSync("storage_key");
      if (value) {
        console.log(value);
        this.canshu = value;
        var num11 = 0;
        var zongjia11 = 0;
        for (let i = 0; i < value.length; i++) {
          num11 = num11 + value[i].num;
          zongjia11 = zongjia11 + value[i].zongjia;
        }
        this.chuhuoshulaing = num11;
        this.chuhuojiage = zongjia11;
      }
    } catch (e) {
      // error
    }
    if (this.canshu == "") {
      this.info = false;
    } else {
      this.info = true;
    }
  },
  mounted() {},
};
</script>
<style scoped lang="less">
.warp_box {
  padding: 0 30rpx;
  background-color: F9F9F9;
  .yihang {
    display: flex;
    height: 60rpx;
    line-height: 60rpx;
    justify-content: space-between;
    border-bottom: 1rpx solid #ddd;
    background-color: #fff;
    margin-top: 20rpx;
    .x1 {
      padding-right: 40rpx;
      text {
        font-size: 30rpx;
        font-weight: 700;
        padding-left: 20rpx;
      }
      img {
        width: 10rpx;
        height: 40rpx;
        vertical-align: middle;
      }
    }
    .x2 {
      padding-right: 40rpx;
      text {
        font-size: 24rpx;
        font-weight: 400;
        padding-right: 20rpx;
      }
      img {
        width: 16rpx;
        height: 24rpx;
        vertical-align: middle;
      }
    }
  }
  .erhang {
    height: 80rpx;
    display: flex;
    justify-content: space-around;
    line-height: 80rpx;
    border-bottom: 1rpx solid #ddd;
    background-color: #fff;

    .left {
      img {
        width: 40rpx;
        height: 40rpx;
        vertical-align: middle;
      }
      text {
        font-weight: 700;
        padding-left: 20rpx;
      }
    }
    .right {
      img {
        width: 40rpx;
        height: 40rpx;
        vertical-align: middle;
      }
      text {
        font-weight: 700;
        padding-left: 20rpx;
      }
    }
  }
  .neironghang {
    margin: 20rpx 0;
    position: relative;
    .neironghangyihang {
      display: flex;
      padding: 0 40rpx;
      .left {
        width: 170rpx;
        height: 170rpx;
        background-color: red;
        img {
          width: 170rpx;
          height: 170rpx;
        }
      }
      .right {
        margin-left: 20rpx;
        width: 300rpx;
        height: 170rpx;
      }
    }
    .neironghangerhang {
      display: flex;
      justify-content: space-around;
      background-color: #fff;
      border-bottom: 1rpx solid #ddd;
      height: 80rpx;
      line-height: 80rpx;
      .x1 {
        width: 100rpx;
      }
      .x2 {
        width: 180rpx;
      }
      .x3 {
        text {
          display: inline-block;
          width: 120rpx;
          text-align: center;
          font-weight: 700;
        }
        img {
          width: 80rpx;
          height: 40rpx;
          vertical-align: middle;
        }
      }
    }
    .neironghangsanhang {
      position: absolute;
      left: -10rpx;
      top: 60rpx;
      width: 40rpx;
      height: 40rpx;
      img {
        width: 40rpx;
        height: 40rpx;
      }
    }
  }
  .sanhang {
    display: flex;
    justify-content: space-between;
    border-bottom: 1rpx solid #ddd;
    background-color: #fff;
    height: 60rpx;
    line-height: 60rpx;
    margin-top: 20rpx;
    .x1 {
      padding-right: 40rpx;
      .xx1 {
        font-size: 30rpx;
        font-weight: 700;
        padding-left: 20rpx;
      }
      img {
        width: 10rpx;
        height: 40rpx;
        vertical-align: middle;
      }
    }
    .x2 {
      padding-right: 40rpx;
      .xx2 {
        font-size: 24rpx;
        font-weight: 400;
        padding-right: 20rpx;
      }
    }
  }
  .sanhang1 {
    display: flex;
    justify-content: space-between;
    border-bottom: 1rpx solid #ddd;
    background-color: #fff;
    height: 60rpx;
    line-height: 60rpx;
    .x1 {
      padding-left: 30rpx;
    }
    .x2 {
      padding-right: 60rpx;
    }
  }
  .sihang {
    border-bottom: 1rpx solid #ddd;
    background-color: #fff;
    height: 60rpx;
    line-height: 60rpx;
    margin-top: 20rpx;
    .x1 {
      padding-right: 40rpx;
      text {
        font-size: 30rpx;
        font-weight: 700;
        padding-left: 20rpx;
      }
      img {
        width: 10rpx;
        height: 40rpx;
        vertical-align: middle;
      }
    }
    textarea {
      margin-top: 1rpx;
      width: 100%;
      background-color: #fff;
      border-radius: 15rpx;
      font-size: 24rpx;
      padding: 15rpx;
    }
  }

  .bottom {
    width: 100%;
    position: fixed;
    bottom: 0;
    left: 0;

    button {
      font-weight: 700;
      width: 90%;
      background-color: #fccd05;
    }
  }
}
</style>
